<?php

namespace App\Http\Controllers\Admin;
use \Hash;
use \DB;
use \Cookie;
use Illuminate\Http\Request;
use App\Http\Requests\LoginInsertRequest;
use App\Http\Requests\LoginUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{

	//进入登录页面
	public function getLogin()
	{
		
		return view('admin.login.login');
	}

	//处理登录数据
	public function postDologin(Request $request)
	{
		
		// dd($request->all());
		//获取用户名
		$name = $request->input('uname');
		$upwd = $request->input('upwd');
		// dd($upwd);
		//进行判断
		if(!$name)
		{
			return back()->with('error','用户名不能为空');
		}
		if(!$request->input('upwd'))
		{
			return back()->with('error','密码不能为空');
		}
		//链接数据库进行查询
		$res = DB::table('shop_users')->where('uname',$name)->first();
		
		//判断用户名
		if(!$res)
		{
			return back()->with('error','用户名或者密码不正确');
		}
		//判断密码
		if(Hash::check($request->input('upwd'), $res->upwd))
		{
			
			session(['uname'=>$res->uname]);
			
			if($request->has('rem'))
				{
					session(['upwd'=>$upwd]);
					session(['remname'=>$name]);
					
				}else{session(['upwd'=>'']);session(['remname'=>'']);}
			
			return redirect('/admin')->with('success','登录成功');
		}else{

			return back()->with('error','用户名或者密码不正确');

		}
	}

	//退出
	public function getLogout(Request $request)
	{
		//清空session
		// $request->session()->flush();
		$request->session()->forget('uname');
		return redirect('admin/login/login')->with('success','退出成功');
	}

}
